home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / class / lowfile.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  5KB  |  225 lines

  1.  
  2.  
  3. /*  Copyright (c) 1993-1996 Algorithms Corporation  */
  4. /*  All rights reserved.  */
  5.  
  6.  
  7.  
  8.  
  9. /*  This file automatically generated by dpp - do not edit  */
  10.  
  11. #define    DPP_STRATEGY    2
  12. #define    DPP_FASTWIDE    0
  13.  
  14.  
  15.  
  16. #line 16 "lowfile.d"
  17. #include <fcntl.h> 
  18. #include <sys/types.h> 
  19. #include <sys/stat.h> 
  20. #ifndef unix 
  21. #include <io.h> 
  22. #endif 
  23.  
  24. #if defined(sparc) || defined(unix) 
  25. #include <unistd.h> 
  26. #define tell(h) lseek(h, 0L, SEEK_CUR) 
  27. #endif 
  28.  
  29. #define    CLASS    LowFile_c
  30. #define    ivType    LowFile_iv_t
  31.  
  32. #include "generics.h"
  33.  
  34. object    LowFile_c;
  35.  
  36.  
  37. #line 38 "lowfile.c"
  38. typedef struct  _LowFile_iv_t  {
  39.     object iName;
  40.     int iHandle;
  41. }    LowFile_iv_t;
  42.  
  43.  
  44.  
  45. #line 36 "lowfile.d"
  46. cmeth objrtn LowFile_cm_gNew(object self)
  47.     return gShouldNotImplement(self, "gNew"); 
  48.  
  49. cmeth objrtn LowFile_cm_gOpenLowFile(object self, char *name, int oflag, int pmode)
  50.     object obj; 
  51.     ivType *iv; 
  52.     int handle; 
  53.  
  54.     if (IsObj((object) name)) 
  55.         name = gStringValue((object) name); 
  56.     handle = open(name, oflag, pmode); 
  57.     if (handle == -1) 
  58.         return NULL; 
  59.     obj = oSuper(LowFile_c, gNew, self)(self); 
  60.     iv = ivPtr(obj); 
  61.     iv->iName = gNewWithStr(String, name); 
  62.     iv->iHandle = handle; 
  63.     return obj; 
  64.  
  65.  
  66. imeth objrtn LowFile_im_gDispose(object self)
  67. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  68.     close(iv->iHandle); 
  69.     gDispose(iv->iName); 
  70.     return oSuper(LowFile_c, gDispose, self)(self); 
  71.  
  72. imeth int LowFile_im_gRead(object self, char *buf, unsigned n)
  73. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  74.     return read(iv->iHandle, buf, n); 
  75.  
  76. imeth int LowFile_im_gWrite(object self, char *buf, unsigned n)
  77. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  78.     return write(iv->iHandle, buf, n); 
  79.  
  80. imeth char * LowFile_im_gGets(object self, char *buf, int sz)
  81. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  82.     int h = iv->iHandle; 
  83.     int i, r; 
  84.     char c; 
  85.  
  86.     if (sz <= 0) 
  87.         return NULL; 
  88.     if (sz-- == 1) { 
  89.         *buf = '\0'; 
  90.         return buf; 
  91.     } 
  92.     for (i=0 ; i < sz ; ) { 
  93.         r = read(h, &c, 1); 
  94.         if (!r && !i || r < 0) 
  95.             return NULL; 
  96.         if (!r) 
  97.             break; 
  98.         buf[i++] = c; 
  99.         if (c == '\n') 
  100.             break; 
  101.     } 
  102.     buf[i] = '\0'; 
  103.     return buf; 
  104.  
  105. imeth long LowFile_im_gAdvance(object self, long n)
  106. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  107.     int h = iv->iHandle; 
  108.     long p = tell(h); 
  109.     long r = lseek(h, n, SEEK_CUR); 
  110.     return r >= 0L ? r-p : 0L; 
  111.  
  112. imeth long LowFile_im_gRetreat(object self, long n)
  113. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  114.     int h = iv->iHandle; 
  115.     long p = tell(h); 
  116.     long r = lseek(h, -n, SEEK_CUR); 
  117.     return r >= 0L ? p-r : 0L; 
  118.  
  119. imeth long LowFile_im_gSeek(object self, long n)
  120. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  121.     long r = lseek(iv->iHandle, n, SEEK_SET); 
  122.     return r; 
  123.  
  124. imeth long LowFile_im_gPosition(object self)
  125. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  126.     return tell(iv->iHandle); 
  127.  
  128. imeth long LowFile_im_gLength(object self)
  129. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  130.     struct stat sb; 
  131.     int r; 
  132.  
  133.     r = fstat(iv->iHandle, &sb); 
  134.     return r ? -1L : sb.st_size; 
  135.  
  136. imeth char * LowFile_im_gName(object self)
  137. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  138.     return gStringValue(iv->iName); 
  139.  
  140. imeth int LowFile_im_gEndOfStream(object self)
  141. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  142. #ifndef unix 
  143.         return eof(iv->iHandle); 
  144. #else 
  145.         gShouldNotImplement(self, "EndOfStream"); 
  146.     iv->iHandle = iv->iHandle; 
  147.     return 0; 
  148. #endif 
  149.     } 
  150.  
  151. imeth int LowFile_im_gFileHandle(object self)
  152. { LowFile_iv_t *iv = GetIVs(LowFile, self);
  153.     return iv->iHandle; 
  154.  
  155.  
  156. #line 172 "lowfile.c"
  157.  
  158. objrtn    LowFile_initialize(void)
  159. {
  160.     static  CRITICALSECTION  cs;
  161.     static  int volatile once = 0;
  162.  
  163.     ENTERCRITICALSECTION(_CI_CS_);
  164.     if (!once) {
  165.         INITIALIZECRITICALSECTION(cs);
  166.         once = 1;
  167.     }
  168.     LEAVECRITICALSECTION(_CI_CS_);
  169.  
  170.     ENTERCRITICALSECTION(cs);
  171.  
  172.     if (LowFile_c) {
  173.         LEAVECRITICALSECTION(cs);
  174.         return LowFile_c;
  175.     }
  176.     INHIBIT_THREADER;
  177.     Stream_initialize();
  178.     if (LowFile_c)  {
  179.         ENABLE_THREADER;
  180.         LEAVECRITICALSECTION(cs);
  181.         return LowFile_c;
  182.     }
  183.     LowFile_c = gNewClass(Class, "LowFile", sizeof(LowFile_iv_t), 0, Stream, END);
  184.     cMethodFor(LowFile, gNew, LowFile_cm_gNew);
  185.     cMethodFor(LowFile, gOpenLowFile, LowFile_cm_gOpenLowFile);
  186.     iMethodFor(LowFile, gRetreat, LowFile_im_gRetreat);
  187.     iMethodFor(LowFile, gPosition, LowFile_im_gPosition);
  188.     iMethodFor(LowFile, gLength, LowFile_im_gLength);
  189.     iMethodFor(LowFile, gWrite, LowFile_im_gWrite);
  190.     iMethodFor(LowFile, gEndOfStream, LowFile_im_gEndOfStream);
  191.     iMethodFor(LowFile, gSeek, LowFile_im_gSeek);
  192.     iMethodFor(LowFile, gAdvance, LowFile_im_gAdvance);
  193.     iMethodFor(LowFile, gDispose, LowFile_im_gDispose);
  194.     iMethodFor(LowFile, gName, LowFile_im_gName);
  195.     iMethodFor(LowFile, gGets, LowFile_im_gGets);
  196.     iMethodFor(LowFile, gGCDispose, LowFile_im_gDispose);
  197.     iMethodFor(LowFile, gDeepDispose, LowFile_im_gDispose);
  198.     iMethodFor(LowFile, gFileHandle, LowFile_im_gFileHandle);
  199.     iMethodFor(LowFile, gRead, LowFile_im_gRead);
  200.  
  201.     ENABLE_THREADER;
  202.  
  203.     LEAVECRITICALSECTION(cs);
  204.  
  205.     return LowFile_c;
  206. }
  207.  
  208.  
  209.  
  210.